package net.kuratkoo.jidelnicekvut;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CanteenListAdapter extends BaseAdapter {

    private static final String TAG = "JidelniceVUT|CanteenListAdapter";
    private Context context;
    private CanteenSet canteenSet;
    private LayoutInflater mInflater;

    public CanteenListAdapter(Context context, CanteenSet canteenSet) {
        this.context = context;
        this.canteenSet = canteenSet;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return this.canteenSet.size();
    }

    public Canteen getItem(int i) {
        return (Canteen) this.canteenSet.get(i);
    }

    public long getItemId(int i) {
        return ((Canteen) this.canteenSet.get(i)).canteenId;
    }

    public View getView(final int i, View convertView, ViewGroup parent) {
        final CanteenViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_canteen, parent, false);

            holder = new CanteenViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.address = (TextView) convertView.findViewById(R.id.address);
            holder.info = (TextView) convertView.findViewById(R.id.info);
            holder.status = (ImageView) convertView.findViewById(R.id.status);
            holder.star = (ImageView) convertView.findViewById(R.id.star);
            convertView.setTag(holder);
        } else {
            holder = (CanteenViewHolder) convertView.getTag();
        }

        holder.name.setText(this.getItem(i).name);
        holder.address.setText(this.getItem(i).address);
        holder.info.setText(this.getItem(i).getFormatedOpeningInfo());
        if (this.getItem(i).isOpen()) {
            if (this.getItem(i).isCloseToClosing()) {
                holder.status.setImageResource(android.R.drawable.presence_away);
            } else {
                holder.status.setImageResource(android.R.drawable.presence_online);
            }
        } else {
            holder.status.setImageResource(android.R.drawable.presence_busy);
        }
        if (this.getItem(i).star) {
            holder.star.setImageResource(android.R.drawable.btn_star_big_on);
        } else {
            holder.star.setImageResource(android.R.drawable.btn_star_big_off);
        }

        final Integer canteenId = this.getItem(i).canteenId;
        holder.star.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                canteenSet.get(i).toggleStar();

                if (CanteenListAdapter.this.canteenSet.get(i).star) {
                    holder.star.setImageResource(android.R.drawable.btn_star_big_on);
                    Toast.makeText(context, context.getText(R.string.canteen) + " '" + canteenSet.get(i).name + "' " + context.getText(R.string.added), Toast.LENGTH_SHORT).show();
                } else {
                    holder.star.setImageResource(android.R.drawable.btn_star_big_off);
                    Toast.makeText(context, context.getText(R.string.canteen) + " '" + canteenSet.get(i).name + "' " + context.getText(R.string.deleted), Toast.LENGTH_SHORT).show();
                }
                canteenSet.getAll();
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
}

class CanteenViewHolder {

    TextView name;
    TextView address;
    TextView info;
    ImageView status;
    ImageView star;
}
